﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuanLyDeTaiKHCN.Obj_class;
namespace QuanLyDeTaiKHCN.UserControls.Manager
{
    public partial class ThanhLapHoiDongXetGiao : System.Web.UI.UserControl
    {
        SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
        _dbDataContext _db = new _dbDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            MemberShip_ASP.listmember = _db.tblHoiDongKhoaHocs.ToList();
            MemberShip_ASP.listdanhmuc = _db.tblDanhMucs.Where(r => r.Loai == 3).ToList();
            if (!IsPostBack)
            {
                string str = Request.QueryString["id"].Replace(" ", "+");
                var item = _db.tblDanhSachDeTaiDuAns.Single(r => r.MaDeTaiDuAn.Trim() == _ps.Decrypt(str, "gold", 128));
                int tongkinhphi = 0;
                try
                {
                    tongkinhphi = int.Parse(item.TongKinhPhi.ToString());
                }
                catch { }
                if (tongkinhphi == 0)
                {
                    lblmessage.Visible = true;
                    lblmessage.Text = "Đề tài này chưa được lập thuyết minh.";
                    pnthanhlaphoidong.Visible = false;
                }
                else
                {
                    lblmessage.Visible = false;
                    pnthanhlaphoidong.Visible = true;
                    inputluu.Value = "chua";
                    CatalogAccess.BindDSHoiDong(drlChuTich);
                    CatalogAccess.BindDSHoiDong(drlPhoChuTich);
                    CatalogAccess.BindDSHoiDong(drlPhanBien1);
                    CatalogAccess.BindDSHoiDong(drlPhanBien2);
                    CatalogAccess.BindDSHoiDong(drlPhanBien3);
                    CatalogAccess.BindDSHoiDong(drlUyVien1);
                    CatalogAccess.BindDSHoiDong(drlUyVien2);
                    CatalogAccess.BindDSHoiDong(drlUyVien3);
                    CatalogAccess.BindDSHoiDong(drlUyVien4);
                    CatalogAccess.BindDSHoiDong(drlUyVien5);
                    CatalogAccess.BindDSHoiDong(drlUyVien6);
                    CatalogAccess.BindDSHoiDong(drlUyVien7);
                    CatalogAccess.BindDSHoiDong(drlUyVien8);
                    CatalogAccess.BindDSHoiDong(drlThuKy);
                    BindData();
                    ListItem itemlinhvuc = new ListItem("---Tất cả---","-1");
                    drlLinhVuc.Items.Add(itemlinhvuc);
                    var listlinhvuc = _db.tblDanhMucs.Where(r => r.Loai == 1);
                    foreach (var itemss in listlinhvuc)
                    {
                        itemlinhvuc = new ListItem(itemss.TenDanhMuc.Trim(), itemss.ID.ToString());
                        drlLinhVuc.Items.Add(itemlinhvuc);
                    }
                }
            }
        }
        protected void BindData()
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            
            string madetai = _db.tblDanhSachDeTaiDuAns.Single(r => r.MaDeTaiDuAn.Trim() == _ps.Decrypt(str, "gold", 128)).MaDeTaiDuAn.Trim();
            try
            {
                var item = _db.tblThanhLapHoiDongs.Single(r => r.MaDeTaiDuAn.Trim() == madetai);
                txtSoQuyetDinh.Text = item.SoQuyetDinh;
                txtNgayRaQuyetDinh.Text = string.Format("{0:dd/MM/yyyy}", item.NgayQuyetDinh);
                string[] st = item.DanhSachHoiDong.Split('@');
                drlChuTich.SelectedValue = st[0];
                drlPhoChuTich.SelectedValue = st[1];
                drlPhanBien1.SelectedValue = st[2];
                drlPhanBien2.SelectedValue = st[3];
                drlPhanBien3.SelectedValue = st[4];
                drlUyVien1.SelectedValue = st[5];
                drlUyVien2.SelectedValue = st[6];
                drlUyVien3.SelectedValue = st[7];
                drlUyVien4.SelectedValue = st[8];
                drlUyVien5.SelectedValue = st[9];
                drlUyVien6.SelectedValue = st[10];
                drlUyVien7.SelectedValue = st[11];
                drlUyVien8.SelectedValue = st[12];
                drlThuKy.SelectedValue = st[13];
                BindDataToTextBox(int.Parse(st[0]), txtchutich);
                BindDataToTextBox(int.Parse(st[1]), txtphochutich);
                BindDataToTextBox(int.Parse(st[2]), txtphanbien1);
                BindDataToTextBox(int.Parse(st[3]), txtphanbien2);
                BindDataToTextBox(int.Parse(st[4]), txtphanbien3);
                BindDataToTextBox(int.Parse(st[5]), txtuyvien1);
                BindDataToTextBox(int.Parse(st[6]), txtuyvien2);
                BindDataToTextBox(int.Parse(st[7]), txtuyvien3);
                BindDataToTextBox(int.Parse(st[8]), txtuyvien4);
                BindDataToTextBox(int.Parse(st[9]), txtuyvien5);
                BindDataToTextBox(int.Parse(st[10]), txtuyvien6);
                BindDataToTextBox(int.Parse(st[11]), txtuyvien7);
                BindDataToTextBox(int.Parse(st[12]), txtuyvien8);
                BindDataToTextBox(int.Parse(st[13]), txtthuky);
                txtSoQuyetThanhLapHoiDong.Text = item.SoQuyetDinhThanhLap;
                txtNgayRaQuyetDinhThanhLapHoiDong.Text = string.Format("{0:dd/MM/yyyy}", item.NgayQuyetDinhThanhLap);
                MemberShip_ASP.madetaiduan = item.MaDeTaiDuAn.Trim();
                inputluu.Value = "luu";
                inputloai.Value = item.tblDanhSachDeTaiDuAn.Loai.ToString();
            }
            catch
            {
                var item = _db.tblDanhSachDeTaiDuAns.Single(r => r.MaDeTaiDuAn.Trim() == madetai);
                txtSoQuyetDinh.Text = "";
                txtNgayRaQuyetDinh.Text = "";
                drlChuTich.SelectedValue = "-1";
                drlPhoChuTich.SelectedValue = "-1";
                drlPhanBien1.SelectedValue = "-1";
                drlPhanBien2.SelectedValue = "-1";
                drlPhanBien3.SelectedValue = "-1";
                drlUyVien1.SelectedValue = "-1";
                drlUyVien2.SelectedValue = "-1";
                drlUyVien3.SelectedValue = "-1";
                drlUyVien4.SelectedValue = "-1";
                drlUyVien5.SelectedValue = "-1";
                drlUyVien6.SelectedValue = "-1";
                drlUyVien7.SelectedValue = "-1";
                drlUyVien8.SelectedValue = "-1";
                drlThuKy.SelectedValue = "-1";
                txtSoQuyetThanhLapHoiDong.Text = "";
                txtNgayRaQuyetDinhThanhLapHoiDong.Text = "";
                inputloai.Value = item.Loai.ToString();
            }
        }
        protected void btnexporttoword_Click(object sender, EventArgs e)
        {

            if (inputluu.Value == "chua")
            {
                lblstatus.Text = "Lưu thông tin trước khi xuất file";
                return;
            }
            else
            {
                if (int.Parse(inputloai.Value) == 1)
                {
                    Response.Redirect("Export/Mau1.aspx");
                }
                else
                {
                    Response.Redirect("Export/Mau2.aspx");
                }
            }
        }
        protected void btnsave_Click(object sender, EventArgs e)
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
            var item = _db.tblDanhSachDeTaiDuAns.Single(r => r.MaDeTaiDuAn.Trim() == _ps.Decrypt(str, "gold", 128));
            string st = "";
            int quyetdinhcua = 0;
            st += drlChuTich.SelectedValue.ToString() + "@";
            st += drlPhoChuTich.SelectedValue.ToString() + "@";
            st += drlPhanBien1.SelectedValue.ToString() + "@";
            st += drlPhanBien2.SelectedValue.ToString() + "@";
            st += drlPhanBien3.SelectedValue.ToString() + "@";
            st += drlUyVien1.SelectedValue.ToString() + "@";
            st += drlUyVien2.SelectedValue.ToString() + "@";
            st += drlUyVien3.SelectedValue.ToString() + "@";
            st += drlUyVien4.SelectedValue.ToString() + "@";
            st += drlUyVien5.SelectedValue.ToString() + "@";
            st += drlUyVien6.SelectedValue.ToString() + "@";
            st += drlUyVien7.SelectedValue.ToString() + "@";
            st += drlUyVien8.SelectedValue.ToString() + "@";
            st += drlThuKy.SelectedValue.ToString() + "@";

            //tinh chi phi
            int tongkinhphi = 0;
            try
            {
                tongkinhphi = int.Parse(item.TongKinhPhi.ToString());
            }
            catch { }
            if (item.LinhVuc == 1)
            {
                if (tongkinhphi <= 600000000)
                    quyetdinhcua = 2;
                else
                    quyetdinhcua = 1;
            }
            else
            {
                if (tongkinhphi <= 300000000)
                    quyetdinhcua = 2;
                else
                    quyetdinhcua = 1;
            }
            bool success;
            if (_db.tblThanhLapHoiDongs.Where(r => r.MaDeTaiDuAn.Trim() == item.MaDeTaiDuAn.Trim()).Count() == 0)
            {
                success = CatalogAccess.InsertHoiDong(item.MaDeTaiDuAn, st, txtSoQuyetDinh.Text, txtNgayRaQuyetDinh.Text,  quyetdinhcua, txtSoQuyetThanhLapHoiDong.Text, txtNgayRaQuyetDinhThanhLapHoiDong.Text);
                if (success)
                {
                    MemberShip_ASP.madetaiduan = item.MaDeTaiDuAn;
                    lblstatus.Text = "Đã lưu.";
                    inputluu.Value = "luu";
                }
                else
                {
                    lblstatus.Text = "Không lưu được.";
                    inputluu.Value = "chua";
                }
            }
            else
            {
                success = CatalogAccess.UpdateHoiDong(item.MaDeTaiDuAn, st, txtSoQuyetDinh.Text, txtNgayRaQuyetDinh.Text,  quyetdinhcua, txtSoQuyetThanhLapHoiDong.Text, txtNgayRaQuyetDinhThanhLapHoiDong.Text);
                if (success)
                {
                    MemberShip_ASP.madetaiduan = item.MaDeTaiDuAn;
                    lblstatus.Text = "Đã lưu.";
                    inputluu.Value = "luu";
                }
                else
                {
                    lblstatus.Text = "Không lưu được.";
                    inputluu.Value = "chua";
                }
            }
        }

        protected void drlChuTich_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlChuTich.SelectedValue.ToString()));
                txtchutich.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtchutich.Text = ""; }
        }

        protected void drlPhoChuTich_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlPhoChuTich.SelectedValue.ToString()));
                txtphochutich.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtphochutich.Text = ""; }
        }

        protected void drlPhanBien1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlPhanBien1.SelectedValue.ToString()));
                txtphanbien1.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtphanbien1.Text = ""; }
        }

        protected void drlPhanBien2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlPhanBien2.SelectedValue.ToString()));
                txtphanbien2.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtphanbien2.Text = ""; }
        }

        protected void drlPhanBien3_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlPhanBien3.SelectedValue.ToString()));
                txtphanbien3.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtphanbien3.Text = ""; }
        }

        protected void drlUyVien1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlUyVien1.SelectedValue.ToString()));
                txtuyvien1.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtuyvien1.Text = ""; }
        }

        protected void drlUyVien2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlUyVien2.SelectedValue.ToString()));
                txtuyvien2.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtuyvien2.Text = ""; }
        }

        protected void drlUyVien3_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlUyVien3.SelectedValue.ToString()));
                txtuyvien3.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtuyvien3.Text = ""; }
        }

        protected void drlUyVien4_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlUyVien4.SelectedValue.ToString()));
                txtuyvien4.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtuyvien4.Text = ""; }
        }

        protected void drlUyVien5_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlUyVien5.SelectedValue.ToString()));
                txtuyvien5.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtuyvien5.Text = ""; }
        }

        protected void drlUyVien6_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlUyVien6.SelectedValue.ToString()));
                txtuyvien6.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtuyvien6.Text = ""; }
        }

        protected void drlThuKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlThuKy.SelectedValue.ToString()));
                txtthuky.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtthuky.Text = ""; }
        }
        protected void BindDataToTextBox(int id, TextBox txt)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == id);
                txt.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txt.Text = ""; }
        }

        protected void drlUyVien7_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlUyVien7.SelectedValue.ToString()));
                txtuyvien7.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtuyvien7.Text = ""; }
        }

        protected void drlUyVien8_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var item = MemberShip_ASP.listmember.Single(r => r.ID == int.Parse(drlUyVien8.SelectedValue.ToString()));
                txtuyvien8.Text = MemberShip_ASP.listdanhmuc.Single(a => a.ID == item.ChucVu).TenDanhMuc.Trim() + " - " + item.NoiCongTac.Trim();
            }
            catch { txtuyvien8.Text = ""; }
        }

        protected void drlLinhVuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlChuTich);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlPhoChuTich);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlPhanBien1);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlPhanBien2);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlPhanBien3);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlUyVien1);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlUyVien2);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlUyVien3);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlUyVien4);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlUyVien5);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlUyVien6);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlUyVien7);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlUyVien8);
            BindLinhVuc(int.Parse(drlLinhVuc.SelectedValue.ToString()), drlThuKy);
        }
        protected void BindLinhVuc(int linhvuc, DropDownList drl)
        {
            if (drl.SelectedValue.ToString() == "-1")
            {
                drl.Items.Clear();
                ListItem item = new ListItem("---Chọn---", "-1");
                drl.Items.Add(item);
                _dbDataContext _db = new _dbDataContext();
                if (linhvuc != -1)
                {
                    var list = _db.tblHoiDongKhoaHocs.Where(r => r.LinhVuc == linhvuc).ToList();
                    foreach (var items in list)
                    {
                        item = new ListItem(items.tblDanhMuc.TenDanhMuc.Trim() + ":" + items.HoTen.Trim(), items.ID.ToString());
                        drl.Items.Add(item);
                    }
                }
                else
                {
                    var list = _db.tblHoiDongKhoaHocs.ToList();
                    foreach (var items in list)
                    {
                        item = new ListItem(items.tblDanhMuc.TenDanhMuc.Trim() + ":" + items.HoTen.Trim(), items.ID.ToString());
                        drl.Items.Add(item);
                    }
                }
            }
        }
    }
}